
package com.example.filtransfer.client;

import com.example.filetransfer.publicsource.Log;
import com.example.filetransfer.publicsource.connection.DataReceiver.onReceiveMessageListener;
import com.example.filetransfer.publicsource.connection.FileTransferListener;
import com.example.filetransfer.publicsource.connection.Protocol;
import com.example.filetransfer.publicsource.fileutils.DirUtils;
import com.example.filetransfer.publicsource.fileutils.MyFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class PcClient {
    private static final String TAG = "PcClient";
    private ClientConnectionManager mConnectionManager;
    private FileVersionUtil mFileVersionUtil;
    private DirectoryWatcher mDirectoryWatcher;

    public void init() {
        try {
            mDirectoryWatcher = new DirectoryWatcher(Protocol.SYNCDIR);
            mDirectoryWatcher.start();
            mConnectionManager = ClientConnectionManager.getInstance();
            mConnectionManager.setMessageListener(mMessageListener);
            mConnectionManager.setFilterTransferListener(mFileTransferListener);
            mFileVersionUtil = new FileVersionUtil();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "fatal exception when create server socket on port :" + Protocol.DEFAULT_SERVER_PORT);
        }
    }

    private void sendFiles(List<MyFile> files) {
        for (MyFile file : files) {
            Log.d(TAG, "send file :" + file.getFilePath());
            try {
                //TODO UI here
                mConnectionManager.sendFile(file.getFilePath(), null);
            } catch (FileNotFoundException e) {
                // CODE WILL NEVER AT HERE
            }
        }
        String version = null;
        try {
            version = mFileVersionUtil.writeVerion(null);
        } catch (IOException e) {
        }
        sendMessage(Protocol.REPLY_SEND_FILE_DONE_PREFIX + version);
    }

    private void sendMessage(String message) {
        mConnectionManager.sendData(message);
    }

    private boolean mCurrentNew = false;

    private onReceiveMessageListener mMessageListener = new onReceiveMessageListener() {
        @Override
        public void receiveMessage(String message) {
            Log.d(TAG, "receive message:" + message);
            if (Protocol.MESSAGE_SEND_ME_YOUR_ALL_FILES.equals(message)) {
                sendFiles(DirUtils.listDirFiles(Protocol.SYNCDIR));
            } else if (Protocol.MESSAGE_SEND_ME_YOUR_FILE_VERSION.equals(message)) {
                String version = null;
                try {
                    version = mFileVersionUtil.getVersion();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                sendMessage(Protocol.REPLY_MY_FILE_VERSION + version);
            } else if (Protocol.MESSAGE_CLIENT_NEW.equals(message)) {
                mCurrentNew = false;
            } else if (Protocol.MESSAGE_SERVER_NEW.equals(message)) {
                mCurrentNew = true;
            } else if (Protocol.MESSAGE_SEND_MY_FILETREE_DONE.equals(message)) {
                //TODO compare filetree here.
            }
        }
    };

    private FileTransferListener mFileTransferListener = new FileTransferListener() {
        @Override
        public void onProgressChanged(String fileUrl, int progress) {
            
        }
    };

    public static void main(String[] args) throws FileNotFoundException {
        PcClient client = new PcClient();
        client.init();
    }
}
